﻿using Script.UI;
using System.Collections;
using System.Collections.Generic;
using LitJson;
using MTUnity.Utils;

public class UI_SPanel : UIUpdateBase
{
    public UI_SortingLine Panel_Sorting;
    public UI_Energy Panel_Energy;
    public UI_Check Panel_Check;


    private FLocation m_fCurrentLocation;

    void Start()
    {
        

    }

    private void OnEnable()
    {
        GlobalEvent.AddEvent(EventName.CurrentEnergyConsumptionUpdate, OnCurrentEnergyConsumptionUpdate);
        GlobalEvent.AddEvent(EventName.CurrentSortingLineDataUpdate, OnCurrentSortingLineDataUpdate);
        GlobalEvent.AddEvent(EventName.CurrentSortingCheckUpdate, OnCurrentSortingCheckUpdate);

        GlobalEvent.AddEvent(EventName.RecordEnergyConsumptionUpdate, OnRecordEnergyConsumptionUpdate);
        GlobalEvent.AddEvent(EventName.RecordSortingLineDataUpdate, OnRecordSortingLineDataUpdate);
        GlobalEvent.AddEvent(EventName.RecordSortingCheckUpdate, OnRecordSortingCheckUpdate);
    }

    public void OnDisable()
    {
        GlobalEvent.RemoveEvent(EventName.CurrentEnergyConsumptionUpdate, OnCurrentEnergyConsumptionUpdate);
        GlobalEvent.RemoveEvent(EventName.CurrentSortingLineDataUpdate, OnCurrentSortingLineDataUpdate);
        GlobalEvent.RemoveEvent(EventName.CurrentSortingCheckUpdate, OnCurrentSortingCheckUpdate);

        GlobalEvent.RemoveEvent(EventName.RecordEnergyConsumptionUpdate, OnRecordEnergyConsumptionUpdate);
        GlobalEvent.RemoveEvent(EventName.RecordSortingLineDataUpdate, OnRecordSortingLineDataUpdate);
        GlobalEvent.RemoveEvent(EventName.RecordSortingCheckUpdate, OnRecordSortingCheckUpdate);
    }

    #region Interface
    public override Dictionary<string, IUIUpdateInterface> RegisterProcessorObj()
    {
        return new Dictionary<string, IUIUpdateInterface>
            {
                {JsonFildName.SortingLine.SortingLineData , Panel_Sorting },
                {JsonFildName.SortingLine.EnergyConsumption , Panel_Energy },
                {JsonFildName.SortingLine.Check , Panel_Check },
            };
    }

    public override Dictionary<string, GatherDel> RegisterGatherMethod()
    {
        return new Dictionary<string, GatherDel>
        {
            //current
            { JsonFildName.SortingLine.Current_EnergyConsumption , GatherCurrentEnergyConsumption},
            { JsonFildName.SortingLine.Current_SortingLineData , GatherCurrentSortlineData},
            { JsonFildName.SortingLine.Current_SortingCheck , GatherCurrentSortingCheck},
            //record
            { JsonFildName.SortingLine.Record_EnergyConsumption , GatherRecordEnergyConsumption},
            { JsonFildName.SortingLine.Record_SortingCount , GatherRecordSortingCount},
            { JsonFildName.SortingLine.Record_SortingDuration , GatherRecordSortingDuration},
            { JsonFildName.SortingLine.Record_SortingEfficient , GatherRecordSortingEfficient},
            { JsonFildName.SortingLine.Record_SortingCheck , GatherRecordSortingCheck},
            //refreshtime
            { JsonFildName.SortingLine.EnergyConsumptionRefreshTime , GatherEnergyConsumptionRefreshTime},
            { JsonFildName.SortingLine.SortingLineDataRefreshTime , GatherSortingLineDataRefreshTime},
            { JsonFildName.SortingLine.CheckRefreshTime , GahterCheckRefreshTime},
        };
    }
	#endregion

	#region UpdateInterface

	public void OnCurrentEnergyConsumptionUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Current_EnergyConsumption,
            JsonFildName.SortingLine.EnergyConsumptionRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }

    public void OnCurrentSortingLineDataUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Current_SortingLineData,
            JsonFildName.SortingLine.SortingLineDataRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }

  
    public void OnCurrentSortingCheckUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Current_SortingCheck,
            JsonFildName.SortingLine.CheckRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }


    public void OnRecordEnergyConsumptionUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Record_EnergyConsumption,
            JsonFildName.SortingLine.EnergyConsumptionRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }

    public void OnRecordSortingLineDataUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Record_SortingEfficient,
            JsonFildName.SortingLine.Record_SortingDuration,
            JsonFildName.SortingLine.Record_SortingCount,
            JsonFildName.SortingLine.SortingLineDataRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }


    public void OnRecordSortingCheckUpdate(object param)
    {
        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Record_SortingCheck,
            JsonFildName.SortingLine.CheckRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }

	#endregion

	public void SetData(FLocation location)
    {
        m_fCurrentLocation = location;

        List<string> gatherDataKey = new List<string>() {
            JsonFildName.SortingLine.Current_EnergyConsumption,
            JsonFildName.SortingLine.Current_SortingLineData,
            JsonFildName.SortingLine.Current_SortingCheck,
            JsonFildName.SortingLine.Record_EnergyConsumption,
            JsonFildName.SortingLine.Record_SortingEfficient,
            JsonFildName.SortingLine.Record_SortingDuration,
            JsonFildName.SortingLine.Record_SortingCount,
            JsonFildName.SortingLine.Record_SortingCheck,
            JsonFildName.SortingLine.EnergyConsumptionRefreshTime,
            JsonFildName.SortingLine.SortingLineDataRefreshTime,
            JsonFildName.SortingLine.CheckRefreshTime,
        };
        JsonData gatherJsonData = DataGather(gatherDataKey);
        DataProcessor(gatherJsonData);
    }

	#region Gather Interface
	public JsonData GatherCurrentEnergyConsumption()
    {
        return new JsonData(FWarehouse.GetInstance().GetCurrentEnergyConsumption(m_fCurrentLocation.ID));
    }

    public JsonData GatherCurrentSortlineData()
    {
        return JsonUtils.GenerateJsonData(((FSortingLine)m_fCurrentLocation.Data).CurrentSortingData);
    }

    public JsonData GatherCurrentSortingCheck()
    {
        int iSortingCheckInPlan, iSortingCheckInCompleted = 0;
        FWarehouse.GetInstance().GetCurrentSortingCheck(m_fCurrentLocation.ID, out iSortingCheckInPlan, out iSortingCheckInCompleted);

        JsonData jsonData = null;
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.SortingLine.SortingCheckInPlan,
            iSortingCheckInPlan
            );
        JsonUtils.GenerateJsonDataWithFieldName(
            ref jsonData,
            JsonFildName.SortingLine.SortingCheckInCompleted,
            iSortingCheckInCompleted
            );
        return jsonData;
    }

    public JsonData GatherRecordEnergyConsumption()
    {
        List<FEnergyConsumptionRecord> listNearest14DData = FWarehouse.GetInstance().GetNearestEnergyConsumption(m_fCurrentLocation.ID, 12);
        return JsonUtils.GenerateJsonData(listNearest14DData);
    }

    public JsonData GatherRecordSortingEfficient()
    {
        List<FSortingEfficientRecord> listNearest14DData = FWarehouse.GetInstance().GetNearestSortingEfficientRecord(m_fCurrentLocation.ID, 14);
        return JsonUtils.GenerateJsonData(listNearest14DData);
    }

    public JsonData GatherRecordSortingDuration()
    {
        List<FSortingDurationRecord> listNearest14DData = FWarehouse.GetInstance().GetNearestSortingDurationRecord(m_fCurrentLocation.ID, 14);
        return JsonUtils.GenerateJsonData(listNearest14DData);
    }

    public JsonData GatherRecordSortingCount()
    {
        List<FSortingCountRecord> listNearest14DData = FWarehouse.GetInstance().GetNearestSortingCountRecord(m_fCurrentLocation.ID, 14);
        return JsonUtils.GenerateJsonData(listNearest14DData);
    }
    public JsonData GatherRecordSortingCheck()
    {
        List<FSortingCheckRecord> listNearest14DData = FWarehouse.GetInstance().GetNearestSortingCheckRecord(m_fCurrentLocation.ID, 14);
        return JsonUtils.GenerateJsonData(listNearest14DData);
    }

    public JsonData GatherEnergyConsumptionRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FSortingLine)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.SortingLine.EnergyConsumptionRefreshTime));
    }

    public JsonData GatherSortingLineDataRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FSortingLine)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.SortingLine.SortingLineDataRefreshTime));
    }

    public JsonData GahterCheckRefreshTime()
    {
        return JsonUtils.GenerateJsonData(((FSortingLine)m_fCurrentLocation.Data).GetRefreshDataTime(JsonFildName.SortingLine.CheckRefreshTime));
    }
    #endregion
}